package com.game.doudizhu.config;

import com.game.doudizhu.model.Player;
import com.game.doudizhu.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Component
@Profile("dev") // 只在开发环境下运行
public class TestDataInitializer implements CommandLineRunner {

    @Autowired
    private PlayerService playerService;

    @Override
    public void run(String... args) {
        // 如果没有测试用户才创建
        if (!playerService.existsByUsername("test1")) {
            createTestPlayers();
        }
    }

    private void createTestPlayers() {
        List<TestPlayerData> testPlayers = Arrays.asList(
            new TestPlayerData("test1", "123456", 1500, 50, 30),
            new TestPlayerData("test2", "123456", 2000, 80, 45),
            new TestPlayerData("test3", "123456", 1800, 65, 35),
            new TestPlayerData("player1", "123456", 1200, 30, 15),
            new TestPlayerData("player2", "123456", 2200, 90, 50),
            new TestPlayerData("player3", "123456", 1600, 40, 20)
        );

        for (TestPlayerData data : testPlayers) {
            Player player = new Player();
            player.setUsername(data.username);
            player.setPassword(data.password);
            player.setScore(data.score);
            player.setGamesPlayed(data.gamesPlayed);
            player.setGamesWon(data.gamesWon);
            player.setLandlordGames(data.gamesPlayed / 2);
            player.setLandlordWins(data.gamesWon / 2);
            player.setFarmerGames(data.gamesPlayed / 2);
            player.setFarmerWins(data.gamesWon / 2);
            player.setHighestScore(data.score + 500);
            player.setRegisterTime(LocalDateTime.now().minusDays(30));
            player.setStatus("ACTIVE");
            player.setOnline(false);

            playerService.savePlayer(player);
        }
    }

    private static class TestPlayerData {
        String username;
        String password;
        int score;
        int gamesPlayed;
        int gamesWon;

        TestPlayerData(String username, String password, int score, int gamesPlayed, int gamesWon) {
            this.username = username;
            this.password = password;
            this.score = score;
            this.gamesPlayed = gamesPlayed;
            this.gamesWon = gamesWon;
        }
    }
} 